package com.coding.v3.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @description 文件工具类
 * @author: <user>
 * @Date: 2023/7/31 15:38
 */
public class FileUtil {

    /**
     * <判断文件夹是否存在，如果存在则清空>
     * @param folderPath
     */
    public static void createFolder(String folderPath) {
        File folder = new File(folderPath);

        if (folder.exists()) {
            System.out.println("文件夹已存在,执行清空操作");
            if (deleteFolderContents(folder)) {
                System.out.println("文件夹已清空：" + folder.getAbsolutePath());
            } else {
                System.out.println("无法清空文件夹：" + folder.getAbsolutePath());
            }


        } else {
            boolean created = folder.mkdirs();
            if (created) {
                System.out.println("文件夹创建成功：" + folder.getAbsolutePath());
            } else {
                System.out.println("文件夹创建失败：" + folder.getAbsolutePath());
            }
        }
    }

    private static boolean deleteFolderContents(File folder) {
        File[] files = folder.listFiles();
        if (files != null) {
            for (File file : files) {
                if (file.isDirectory()) {
                    if (!deleteFolderContents(file)) {
                        return false;
                    }
                }
                if (!file.delete()) {
                    return false;
                }
            }
        }
        return true;
    }


    /**
     * <实现将某一文件夹下的所有文件压缩成一个压缩包并下载到用户的桌面>
     * @param sourceFolderPath
     */
    public static void zipFolder( String sourceFolderPath) {
//        String sourceFolderPath = "src/main/temp/"; // 替换成源文件夹的路径
        String zipFileName = "generateCode"+ System.currentTimeMillis() + ".zip"; // 压缩包的文件名

        File sourceFolder = new File(sourceFolderPath);
        File zipFile = new File(System.getProperty("user.home")+  File.separator + "Desktop", zipFileName);

        try {
            FileOutputStream fos = new FileOutputStream(zipFile);
            ZipOutputStream zos = new ZipOutputStream(fos);

            zipFolder(sourceFolder, sourceFolder.getName(), zos);

            zos.close();
            fos.close();

            System.out.println("压缩完成：" + zipFile.getAbsolutePath());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void zipFolder(File folder, String parentFolderName, ZipOutputStream zos) throws IOException {
        File[] files = folder.listFiles();

        for (File file : files) {
            if (file.isDirectory()) {
                zipFolder(file, parentFolderName + "/" + file.getName(), zos);
            } else {
                byte[] buffer = new byte[1024];
                FileInputStream fis = new FileInputStream(file);
                zos.putNextEntry(new ZipEntry(parentFolderName + "/" + file.getName()));

                int length;
                while ((length = fis.read(buffer)) > 0) {
                    zos.write(buffer, 0, length);
                }

                zos.closeEntry();
                fis.close();
            }
        }
    }
}